package com.cy.hui_jie_dan.service.impl;

import com.cy.hui_jie_dan.comon.constant.taskIdPassConstant;
import com.cy.hui_jie_dan.comon.exception.taskIdExcpetion;
import com.cy.hui_jie_dan.comon.result.PageResult;
import com.cy.hui_jie_dan.mapper.EditProfileMapper;
import com.cy.hui_jie_dan.mapper.TaskMapper;
import com.cy.hui_jie_dan.mapper.UserMapper;
import com.cy.hui_jie_dan.popj.dto.taskDTO;
import com.cy.hui_jie_dan.popj.entity.EditProfile;
import com.cy.hui_jie_dan.popj.entity.Task;
import com.cy.hui_jie_dan.popj.vo.tasksListVO;
import com.cy.hui_jie_dan.service.taskService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class taskServiceImpl implements taskService {
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    EditProfileMapper editProfileMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public PageResult tasksList(taskDTO taskDTO) {
        PageHelper.startPage(taskDTO.getPage(),taskDTO.getPageSize());
        Page<Task> page = taskMapper.selectByList(taskDTO);
        long total = page.getTotal();
        List<tasksListVO> result =page.getResult().stream().map((item) -> {
            tasksListVO listVO = new tasksListVO();
            BeanUtils.copyProperties(item, listVO);
            EditProfile editProfile = editProfileMapper.selectByUserId(item.getUserId());
            listVO.setCompanyName(editProfile.getCompanySchool());
            return listVO;
        }).collect(Collectors.toList());
        return new PageResult(total,result);
    }

    @Override
    public tasksListVO taskDetail(Integer id) {
        if (id==null){
            throw new taskIdExcpetion(taskIdPassConstant.SET_TASKID_em);
        }
        Task task = taskMapper.selectByPrimaryKey(id);
        if(task==null){
            throw new taskIdExcpetion(taskIdPassConstant.SET_TASKID_EM);
        }
        tasksListVO taskListVO = new tasksListVO();
        BeanUtils.copyProperties(task,taskListVO);
        tasksListVO tasksListVO1=userMapper.selectById(taskListVO.getUserId());
        taskListVO.setCompanyName(tasksListVO1.getCompanyName());
        taskListVO.setCommunityPhone(tasksListVO1.getCommunityPhone());
        taskListVO.setCommunityAvatar(taskListVO.getCommunityAvatar());
        return taskListVO;
    }
}
